﻿/* $Author: alexander@bsrgin.ru $
 * $Id: LayoutControlButtons.java 8 2011-11-13 20:11:06Z alexander@bsrgin.ru $
 * 
 */
package ru.habrahabr.prettyalarm;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.Button;

public class LayoutControlButtons extends ViewGroup
{
	//private static float dip_scale;
	private static final double CONTENT_LAYOUT_BY_WIDTH = 0.80;
	private static int main_btn_width;

	public LayoutControlButtons(Context context)
	{
		super(context, null);
		init(context);
	}

	public LayoutControlButtons(Context context, AttributeSet attrs)
	{
		super(context, attrs, 0);
		init(context);
	}

	public LayoutControlButtons(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context)
	{
		//dip_scale = context.getResources().getDisplayMetrics().density;
		main_btn_width = (int)context.getResources().getDimension(R.dimen.main_btn_width);
	}

	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b)
	{
		final int width = getWidth();
		final int height = getHeight();
		final int content_width = (int)(width * CONTENT_LAYOUT_BY_WIDTH);
		final int btns_top_offset = (height - main_btn_width) / 2;
		final int btns_left_offset = (width - content_width) / 2; 
		final int btns_padding = (content_width-3*main_btn_width)/3;
		final int count = getChildCount();
		
		for (int i = 0, padding = btns_padding/2; i < count; i++, padding+=btns_padding)
		{
			final Button btn = (Button)getChildAt(i);
			if (btn.getVisibility() == GONE) continue;
			btn.layout(
					btns_left_offset+i*main_btn_width+padding,
					btns_top_offset,
					btns_left_offset+(i+1)*main_btn_width+padding,
					btns_top_offset+main_btn_width+30);
		}
	}	
}
